var app = getApp();
Page({
    data:{
        curPage : '/zofui_sales/shop/trademap',
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        isIPX : app.globalData.isIPX,
        info : {},
        morecard : false,
        tabtype : 0,
        params : {},
        latitude : 113,
        longitude : 23,
        scale : 16,
        mapTheme: "handDraw",
        markers : [],
        includePoints : [],
        map : null,
        tradelist : [],
        actid : '0',
        isearch : false,
        search : '',
        isgeting : false,
        actshop : {},
        showinfo : false,
        allsort : [],
    },
    onLoad:function(options){
        var self = this;
        if( options.scene ){
            var list = options.scene.split('_');
            options.tid = list[0] ? list[0] : 0;
            options.zfid = list[1] ? list[1] : 0;
            options.sortid = list[2] ? list[2] : 0;
        }

        var tid = options.tid ? options.tid : ( options.scene ? options.scene : 0 );
        var zfid = options.zfid ? options.zfid : '';
        var sortid = options.sortid ? options.sortid : '';
        self.data.options = options;
        
        app.com.getLocation(self,function(loc){

            self.setData({
                latitude : loc.latitude,
                longitude : loc.longitude,
            })

            app.com.auth(function(){

                var postdata = {
                    op : 'trademap',
                    id : tid,
                    sortid : sortid,
                    zfid : zfid,
                    lat : loc.latitude,
                    lng : loc.longitude,
                    isinit : 1,
                }

                app.com.http('shop_v1','POST',postdata,0,false,'','',function(res){
                    if (res.data && !res.data.errno) {

                        self.setData({
                            inited : true,
                            actid : postdata.id,
                            info : res.data.data.info ? res.data.data.info : {},
                            markers : res.data.data.markers ? res.data.data.markers : [],
                            tradelist : res.data.data.tradelist ? res.data.data.tradelist : [],
                            allsort : res.data.data.allsort ? res.data.data.allsort : [],
                            sharedata : res.data.data.sharedata,
                            params : res.data.data.params,
                        },function(){
                            self.data.map = wx.createMapContext("myMap");
                            if(res.data.data.includePoints && res.data.data.includePoints.length > 0){
                                self.data.map.includePoints({
                                    padding: [ 60, 30, 60, 30],
                                    points: res.data.data.includePoints
                                })
                            }

                        })

                        wx.setNavigationBarTitle({
                            title: res.data.data.title
                        });
                        
                    }else{
                        app.com.alert(res.data.message,function(){
                            app.com.backPage(options.zfid);
                        });
                    }
                });
                app.com.setBar(app,self);
            });
        })
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.onLoad( this.data.options );
        this.data.isdown = false;

        wx.stopPullDownRefresh();
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onTypeTabItem(e){
        if(this.data.isgeting){
            return false;
        }
        this.data.isgeting = true;

        var self = this;
        var id = e.currentTarget.dataset.id;
        var type = e.currentTarget.dataset.type*1;
        var postdata = {
            op : 'trademap',
            id : self.data.options.tid ? self.data.options.tid : '',
            sortid : id ? id : '',
            lat : self.data.latitude,
            lng : self.data.longitude,
            search : self.data.search ? self.data.search : '',
        }

        var isload = type == 0 || type == 2 ? true : false;

        app.com.http('shop_v1','POST',postdata,0, isload ,'','',function(res){
            if (res.data && !res.data.errno) {
                self.setData({
                    actid : postdata.sortid,
                    markers : res.data.data.markers ? res.data.data.markers : [],
                    showinfo : false,
                },function(){
                    if(isload && self.data.map && res.data.data.includePoints && res.data.data.includePoints.length > 0){
                        self.data.map.includePoints({
                            padding: [ 60, 30, 60, 30],
                            points: res.data.data.includePoints
                        })
                    }
                    self.data.isgeting = false;
                })
            }
        });

    },
    onMarkertap(e){
        var self = this;
        var id = e.detail.markerId;
        for (var i = 0; i < this.data.markers.length; i++) {
            if(this.data.markers[i].id*1 == id*1){
                self.setData({
                    actshop : this.data.markers[i],
                    showinfo : true,
                })
            }
        }
    },
    mapRegionChange(e){
        if(e.type == 'end' && e.causedBy == 'drag'){
            var tar = {
                currentTarget : {
                    dataset : {
                        id : this.data.actid,
                        type : 1,
                    }
                }
            }
            this.onTypeTabItem(tar);
        }
    },
    onMapTab(e){
        this.setData({
            showinfo : false,
        })
    },
    searchstart(){
        this.setData({
            isearch : !this.data.isearch,
        })
        this.data.search = '';
    },
    searchshopfn(){
        var search = this.data.search ? this.data.search : '';
        var tar = {
            currentTarget : {
                dataset : {
                    id : this.data.actid,
                    type : 2,
                }
            }
        }
        this.onTypeTabItem(tar);
    },
    clears(){
        this.data.search = '';
        this.searchshopfn();
    },
    changekey(e){
        this.data.search = e.detail;
    },
    back(){
        app.com.backPage();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e,this.data.tid );
    },
    redirectto : function( e ){
        app.com.redirectto( e,this.data.tid );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    showJoin(e){
        var self = this;
        self.setData({
            Joinanimation : !self.data.Joinanimation,
        })
    },
    
})